/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "client.h"
#include "webdir.h"
#include "partition.h"

int 
construct_web_directory(mainFrame *mf, tgrFrame *tgf,char *filename,
			char *url_base)
{
  tgrFrame *wdir_tgf = create_directed_graph(mf);
  GraphFrame *gf = tgf->gf;
  GraphFrame *wgf = wdir_tgf->gf;

  LNode_type *ptr, *lver = get_list_vertices(gf);
  
  for(ptr = lver->back; ptr!= lver ; ptr= ptr->back)
    {
      struct pt *v = (struct pt *)ptr->info;

      if(get_shape(v) == BOX)
	{
	  LNode_type *lp,*list = init_linked_list();
	  char *vlabel = get_label_vertex(v);
	  struct pt *pnv=NULL, *nv;

	  if(partition_url(vlabel, list,url_base))
	    {
	      Delete_linked_list(list);
	      continue;
	    }

	  for(lp = list->back; lp != list ; lp = lp->back)
	    {
	      int es = (int)lp->info;
	      char * nlabel = malloc(es+2);
	      if(!nlabel) {
		  fprintf(stderr,"Out of Memory construct_web_directory\n");
		  abort();
	      }

	      strncpy(nlabel,vlabel, es+1);
	      nlabel[es+1]='\0';
	      nv = find_vertex_by_name(wgf, nlabel);
	      if(!nv)
		{
		  char sn_str[10];
		  int sn;
		  char *tmpl = malloc(strlen(nlabel) + 2);
		  if(!tmpl) {
		      fprintf(stderr,"Out of Memory construct_web_directory\n");
		      abort();
		  }

		  sprintf(tmpl, "%s*", nlabel);
		  sn = select_nodes_by_string_template(gf,tmpl);
		  free(tmpl); tmpl = NULL;
		  sprintf(sn_str,"%d",sn);
		  deselect_all_nodes(gf);

		  nv = create_node_in_graph_name(wgf, nlabel);
		  change_weight_vertex(wgf, nv,sn_str);

		  if (pnv)
		    adding_edge(wgf, pnv, nv, "1");
		  else 
		    wdir_tgf->root = nv;

		}
	      pnv = nv;
	      free(nlabel); nlabel = NULL;
	    }
	  Delete_linked_list(list);
	}
    }

  if(wdir_tgf->root)
    {
      set_vertex_position(wdir_tgf->root, GR_MWIDTH/2, 20);
      compute_tree_positions(wdir_tgf->gf,wdir_tgf->root,NORTH);

      if(!wdir_tgf->filename)
	  GRStrAllocCopy(wdir_tgf->filename,filename);
      save_current_graph(wdir_tgf);
    }

  return 0;
}


int
partition_url(char *url, LNode_type *list, char *url_base)
{
  int i,ulen = url ? strlen(url) : 0;
  int l = strlen(url_base);

  if(ulen == 0)
    return -1;
  
  if(!list)
    return -1;

  if(strncmp(url,url_base, l))
    return -1;
  
  for(i = l; i< ulen ; i++)
    {
      if(url[i]=='/')
	Insert_linked_list(list, create_new_node_llist((char *)i));
    }
  return 0;
}






